<?php

/**
 * Class UserActivation_Model
 */
class UserActivation_Model extends CI_Model
{
    /**
     *
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param int $userId
     * @return string|null
     */
    public static function getActivationCodeByUserId($userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        self::GC();

        $CI->db->select('user_activation.*');
        $CI->db->from('user_activation');
        $CI->db->where('user_activation.user_id', $userId);

        $query = $CI->db->get();
        $row   = $query->row();

        if ($row)
            return $row->activation_code;
        else
            return null;
    }

    public static function getUserIdAndDeleteItSelfByActivationCode($activationCode)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        //先GC，清掉过期的激活码
        self::GC();

        $CI->db->select('user_activation.*');
        $CI->db->from('user_activation');
        $CI->db->where('user_activation.activation_code', $activationCode);

        $query = $CI->db->get();
        $row   = $query->row();

        if ($row) {
            self::deleteById($row->id);
            return $row->user_id;
        } else {
            return null;
        }
    }

    /**
     * @param int $userId
     * @param string $activationCode
     * @return object
     */
    public static function update($userId, $activationCode)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        if (self::isUserIdExist($userId)) {
            $CI->db->where('user_activation.user_id', $userId);
            $result = $CI->db->update('user_activation',
                array(
                    'activation_code'  => $activationCode,
                    'time_of_validity' => date("Y-m-d H:i:s", time() + 24 * 60 * 60),
                ));
        } else {
            $result = $CI->db->insert('user_activation',
                array(
                    'user_id'          => $userId,
                    'activation_code'  => $activationCode,
                    'time_of_validity' => date("Y-m-d H:i:s", time() + 24 * 60 * 60),
                )
            );
        }

        self::GC();

        return $result;
    }

    /**
     * @param int $id
     * @return bool
     */
    public static function deleteById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        self::GC();

        $CI->db->where('user_activation.id', $id);
        $result = $CI->db->delete('user_activation');

        if ($result)
            return true;
        else
            return false;
    }

    public static function isUserIdExist($userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('user_activation.*');
        $CI->db->from('user_activation');
        $CI->db->where('user_activation.user_id', $userId);
        if ($CI->db->get()->num_rows())
            return true;
        else
            return false;
    }

    public static function isActivationCodeExist($activationCode)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        self::GC();

        $CI->db->select('user_activation.*');
        $CI->db->from('user_activation');
        $CI->db->where('user_activation.activation_code', $activationCode);
        if ($CI->db->get()->num_rows())
            return true;
        else
            return false;
    }

    public static function GC()
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('user_activation.time_of_validity <', date("Y-m-d H:i:s", time()));
        $CI->db->delete('user_activation');
    }

}
